<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图纸编号" prop="drawCode">
        <el-input
          v-model="queryParams.drawCode"
          placeholder="请输入图纸编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图纸类型" prop="drawType">
        <el-select v-model="queryParams.drawType" placeholder="请选择图纸类型" clearable>
          <el-option
            v-for="dict in dict.type.draw_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设计人" prop="designer">
        <el-input
          v-model="queryParams.designer"
          placeholder="请输入设计人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图纸状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择图纸状态" clearable>
          <el-option label="草稿" :value="0"></el-option>
          <el-option label="启用" :value="1"></el-option>
          <el-option label="禁用" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:draw:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drawList">
      <el-table-column label="图纸编号" align="center" prop="drawCode" width="140">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['md:draw:query']"
          >{{scope.row.drawCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="图纸名称" align="center" prop="drawName" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="版本号" align="center" prop="version" width="160" :show-overflow-tooltip="true" />
      <el-table-column label="图纸类型" align="center" prop="drawType" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.drawType === 1" type="success">产品图纸</el-tag>
          <el-tag v-else-if="scope.row.drawType === 2">工序图纸</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="itemCode" width="140"/>
      <el-table-column label="产品名称" align="center" prop="itemName" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="设计人" align="center" prop="designer" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="设计时间" align="center" prop="designDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.designDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图纸状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">启用</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-tooltip content="查看" placement="top">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['md:draw:query']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="启用" placement="top">
            <el-button
              size="mini"
              type="success"
              icon="el-icon-circle-check"
              @click="handleUse(scope.row)"
              v-hasPermi="['md:draw:use']"
              v-if="scope.row.status === 0 || scope.row.status === 2"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="禁用" placement="top">
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-circle-close"
              @click="handleStop(scope.row)"
              v-hasPermi="['md:draw:stop']"
              v-if="scope.row.status === 1"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['md:draw:remove']"
              v-if="scope.row.status === 0"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图纸管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="图纸编号" prop="drawCode">
              <el-input v-model="form.drawCode" placeholder="请输入图纸编号" :readonly="optType == 'view'" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)"
                         v-if="optType != 'view'"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图纸名称" prop="drawName">
              <el-input v-model="form.drawName" placeholder="请输入图纸名称" :readonly="optType == 'view'" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请输入产品编码" :readonly="optType == 'view'" >
                <el-button slot="append" @click="handleSelectProduct" icon="el-icon-search" v-if="optType != 'view'"></el-button>
              </el-input>
            </el-form-item>
            <ItemSelect ref="itemSelect" @onSelected="onItemSelected" > </ItemSelect>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入产品名称" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version">
              <el-input v-model="form.version" placeholder="请输入版本号" :readonly="optType == 'view'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图纸类型" prop="drawType">
              <el-select v-model="form.drawType" placeholder="请选择图纸类型" clearable :disabled="optType == 'view'">
                <el-option
                  v-for="draw in drawOptions"
                  :key="draw.value"
                  :label="draw.label"
                  :value="draw.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设计人" prop="designer">
              <el-input v-model="form.designer" placeholder="请输入设计人" :readonly="optType == 'view'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设计时间" prop="designDate">
              <el-date-picker
                clearable
                v-model="form.designDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择设计时间"
                :disabled="optType == 'view'"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="图纸描述" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :readonly="optType == 'view'" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" v-if="optType != 'view'">
            <el-form-item label="图纸" prop="drawUrl">
              <FileUpload
                :limit="1"
                :fileSize="100"
                :fileType="fileType"
                :value="pictureList"
                :optType="optType"
                :isShowTip="isShowTip"
                @onUploaded="handleUploaded"
                @onRemoved="handleRemoved"
              >
              </FileUpload>
              <el-input v-model="form.drawUrl" v-show="false" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="optType == 'view'">
            <iframe
              :src="form.drawUrl"
              frameborder="0"
              scrolling="auto"
              style="width: 100%; height: 600px;"
            />
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType != 'view'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDraw, getDraw, delDraw, addDraw, updateDraw, useDraw, stopDraw } from "@/api/mes/md/draw";
import { genCode } from "@/api/system/autocode/rule";
import ItemSelect  from "@/components/itemSelect/single.vue";
import FileUpload from "@/components/FileUpload/index";

export default {
  name: "Draw",
  dicts: ['draw_type'],
  components: { ItemSelect, FileUpload, },
  data() {
    return {
      //自动生成编码
      autoGenFlag:false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图纸管理表格数据
      drawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显隐标识
      optType: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drawCode: null,
        designer: null,
        drawType: null,
        itemCode: null,
        itemName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 上传类型
      fileType: ["pdf"],
      // 图纸列表
      pictureList: [],
      // 上传提示开关
      isShowTip: true,
      // 表单校验
      rules: {
        drawCode: [
          { required: true, message: "图纸编号不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ],
        drawUrl: [
          { required: true, message: "图纸不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "关联产品ID不能为空", trigger: "blur" }
        ],
        itemCode: [
          { required: true, message: "关联产品编码不能为空", trigger: "blur" }
        ],
        itemName: [
          { required: true, message: "关联产品名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "图纸状态不能为空", trigger: "blur" }
        ],
      },
      drawOptions: [
        {value: 1, label: "产品图纸"},
        {value: 2, label: "工序图纸"}
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图纸管理列表 */
    getList() {
      this.loading = true;
      listDraw(this.queryParams).then(response => {
        this.drawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        drawCode: null,
        drawName: null,
        version: null,
        designer: null,
        designDate: null,
        drawType: null,
        drawUrl: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        status: 0,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.autoGenFlag = false;
      this.pictureList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图纸管理";
      this.optType = "add";
      this.isShowTip = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDraw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDraw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除编号为："' + row.drawCode + '"的图纸？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDraw(id).then(response => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
      }).catch(() => {
        this.$modal.msgError("取消删除");
        return false;
      });
    },
    /** 查看 **/
    handleView(row) {
      this.reset();
      const id = row.id
      getDraw(id).then(response => {
        this.form = response.data;
        this.pictureList.push(response.data.drawUrl);
        this.open = true;
        this.title = "查看图纸管理";
        this.optType = 'view';
        this.isShowTip = false;
      });
    },
    /** 启用 **/
    handleUse(row) {
      this.$modal.confirm('是否启用编号为："' + row.drawCode + '"的图纸？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.status = 1;
        useDraw(row).then(response => {
          this.$modal.msgSuccess("启用成功！");
          this.getList();
        })
      }).catch(() => {
        this.$modal.msgError("取消启用！");
        return false;
      })
    },
    /** 禁用 **/
    handleStop(row) {
      this.$modal.confirm('是否禁用编号为："' + row.drawCode + '"的图纸？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.status = 2;
        stopDraw(row).then(response => {
          this.$modal.msgSuccess("禁用成功！");
          this.getList();
        })
      }).catch(() => {
        this.$modal.msgError("取消禁用！");
        return false;
      })
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag){
      if(autoGenFlag){
        genCode('DRAW_CODE').then(response =>{
          this.form.drawCode = response;
        });
      }else{
        this.form.drawCode = null;
      }
    },
    // 产品物料选择开关
    handleSelectProduct(){
      this.$refs.itemSelect.showFlag = true;
    },
    // 产品物料选择弹出框
    onItemSelected(obj){
      if(obj != undefined && obj != null){
        this.form.itemId = obj.itemId;
        this.form.itemCode = obj.itemCode;
        this.form.itemName = obj.itemName;
      }
    },
    // 上传成功
    handleUploaded(data) {
      this.form.drawUrl = data;
    },
    // 删除文件
    handleRemoved(data) {
      this.form.drawUrl = data;
    },
    /** 导出按钮操作 */
    /*handleExport() {
      this.download('md/draw/export', {
        ...this.queryParams
      }, `draw_${new Date().getTime()}.xlsx`)
    }*/
  }
};
</script>
